
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
#include "ThreadPool/Log.hpp"

void cline_server(int cline_socket)
{
    
    char buff[1024];
    while (1)
    {
        std::string message;
        std::cerr << "请输入内容#";
        std::getline(std::cin, message);
        // 向网络套接字中写消息，我们也可以使用send借口，sendto是给udb协议使用的接口
        write(cline_socket, message.c_str(), message.size());
        
    bzero(&buff, sizeof buff);
    // 或者用 recv接口
    ssize_t ret = read(cline_socket, buff, (sizeof buff) - 1);
     if (ret < 0 )
     {
            //出现错误
        Logmessage(FATIL, "read falt");
        break;
     }else if (ret == 0)
     {
        //管道关闭
        Logmessage(FATIL, "pipe close");
        break;
     }else
      {
        //读取成功
        buff[ret] = 0;
        printf("server echo #%s\n", buff);
     }
    }
}

int main(int argc, char* args[])
{
     if (argc != 3)
    {
        [args]{printf("%s 需要目的IP port\n", args[0]);}();
        exit(1);
    }
    int cline_socket = -1;
    cline_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (cline_socket < 0)
    {
        Logmessage(FATIL, "cline socket fail %d %s", errno, strerror(errno));
        exit(1);
    }
    // 1.保留目标信息
    struct sockaddr_in goal_service;
    bzero(&goal_service, sizeof goal_service);
    goal_service.sin_family = AF_INET;
    goal_service.sin_port = htons(atoi(args[2]));
    goal_service.sin_addr.s_addr = inet_addr(args[1]);
    // 2.建立连接
    if (connect(cline_socket, (sockaddr*)&goal_service, sizeof goal_service) < 0)
    {
        Logmessage(FATIL, "cline connect fail %d %s", errno, strerror(errno));
        exit(1);
    }
    // 3.发送信息
    // 4.回收信息
    cline_server(cline_socket);
    close(cline_socket);

    return 0;
}









